package program;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Administrator on 2016/12/7.
 */
public class MySQL {
    private static Connection getConn() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/javamail";
        String username = "root";
        String password = "weirui199748";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
     public static String[] getAddress()throws Exception{
         Connection conn = getConn();
         String sql = "select address from addresses";
         PreparedStatement pstmt;
         pstmt = (PreparedStatement)conn.prepareStatement(sql);
         ResultSet rs = pstmt.executeQuery();
         int col = rs.getMetaData().getColumnCount();
         String[] addresses=new String[col];
         while (rs.next()) {
             for (int i = 1; i <= col; i++) {
                     addresses[i-1]=rs.getString(i);
             }
         }
         return addresses;
     }
}
